<template>
  <div>
    <el-container>
      <el-main>
        <div style="margin-bottom: 20px;text-align: left" >
          <el-input style="width: 240px;" placeholder="请输入名称" v-model="params.donors"></el-input>
          <el-input style="width: 240px; margin-left: 15px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
          <el-button style="margin-left: 15px" type="primary" @click="getall"><i class="el-icon-search"></i> 搜索</el-button>
          <el-button style="margin-left: 15px" type="warning" @click="reset"><i class="el-icon-refresh"></i> 重置</el-button>
        </div>
<!--        <div style="text-align: left" >-->
<!--            <el-button style="margin-left: 0px" size="small" type="primary" @click="addinfo"><i class="el-icon-circle-plus-outline"></i>     添加信息</el-button>-->
<!--        </div>-->
        <el-table
            :border="true"
            :data="tableData"
            stripe
            style="width: 100%;margin-top: 20px"
        >
          <el-table-column
              prop="id"
              label="编号"
              width="100">
          </el-table-column>
          <el-table-column
              prop="donors"
              label="捐献人"
              width="180">
          </el-table-column>
          <el-table-column
              prop="sex"
              label="性别(支持筛选)"
              width="180"
              :filters="[{ text: '男', value: '男' }, { text: '女', value: '女' }]"
              :filter-method="filterTag"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              <el-tag
                  :type="scope.row.sex === '男' ? 'primary' : 'success'"
                  disable-transitions>{{scope.row.sex}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              prop="bloodType"
              label="血型(支持筛选)"
              width="180"
              :filters="[{ text: 'A型', value: 'A' }, { text: 'B型', value: 'B' }, { text: 'O型', value: 'O' }, { text: 'AB型', value: 'AB' }]"
              :filter-method="filterHandler"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              {{scope.row.bloodType}} 型
            </template>
          </el-table-column>
          <el-table-column
              prop="age"
              label="年龄"
              width="180">
            <template slot-scope="scope">
              {{scope.row.age}} 岁
            </template>
          </el-table-column>
          <el-table-column
              prop="phone"
              label="联系方式"
              width="180">
          </el-table-column>
          <el-table-column label="操作"  width="200">
            <template #default="scope">
              <!--          scope.row 就是当前行数据-->
              <el-button type="primary" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i>  编 辑</el-button>
              <el-popconfirm
                  style="margin-left: 5px"
                  title="您确定删除这行数据吗？"
                  @confirm="del(scope.row.id)"
              >
                <el-button type="danger" size="small" slot="reference"><i class="el-icon-delete"></i>  删 除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--    分页-->
        <div style="margin-top: 20px">
          <el-pagination
              background
              :current-page="params.pageNum"
              :page-size="params.pageSize"
              layout="prev, pager, next, jumper"
              @current-change="handleCurrentChange"
              :total="total">
          </el-pagination>
        </div>
        <!--      修改-->
        <div style="margin: 10px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleEdit"
              title="修改"
              width="30%"
              center>
            <el-form :model="form" ref="ruleForm" :rules="rules" label-width="60px">
              <el-form-item label="捐献人">
                <el-input v-model="form.donors" style="width: 85%" disabled/>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="9">
                  <el-form-item label="血型">
                    <el-select v-model="form.bloodType" style="width: 102%" placeholder="请选择">
                      <el-option
                          v-for="item in bloodTypes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="15">
                  <el-form-item label="性别">
                    <el-select v-model="form.sex" style="width: 72%" placeholder="请选择">
                      <el-option
                          v-for="item in sexes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="9">
                  <el-form-item label="年龄">
                    <el-input v-model="form.age" style="width: 102%"/>
                  </el-form-item>
                </el-col>
                <el-col :span="15">
                  <el-form-item label="电话">
                    <el-input v-model="form.phone" style="width: 72%"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleEdit = false">取消</el-button>
                  <el-button type="primary" @click="save">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>

        <!--      添加-->
        <div style="margin: 10px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleAdd"
              title="添加"
              width="30%"
              center>
            <el-form :model="addform" ref="ruleForm" :rules="rules" label-width="60px">
              <el-form-item label="捐献人">
                <el-input v-model="addform.donors" style="width: 85%"/>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="9">
                  <el-form-item label="血型">
                    <el-select v-model="addform.bloodType" style="width: 102%" placeholder="请选择">
                      <el-option
                          v-for="item in bloodTypes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="15">
                  <el-form-item label="性别">
                    <el-select v-model="addform.sex" style="width: 72%" placeholder="请选择">
                      <el-option
                          v-for="item in sexes"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="9">
                  <el-form-item label="年龄">
                    <el-input v-model="addform.age" style="width: 102%"/>
                  </el-form-item>
                </el-col>
                <el-col :span="15">
                  <el-form-item label="电话">
                    <el-input v-model="addform.phone" style="width: 72%"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleAdd = false">取消</el-button>
                  <el-button type="primary" @click="confirmadd">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  data(){
    return{
      tableData: [],
      total:0,
      params: {
        pageNum: 1,
        pageSize: 7,
        donors: '',
        phone: ''
      },
      form:{
        donors: '',
        sex:'',
        bloodType:'',
        age:'',
        phone: ''
      },
      addform:{
        donors: '',
        sex:'',
        bloodType:'',
        age:'',
        phone: ''
      },
      sex:'',
      bloodType:'',
      rules: {
        age: [{required: true, message: '请输入年龄', trigger: 'blur'}],
        phone: [{required: true, message: '请输入电话', trigger: 'blur'}],
      },
      centerDialogVisibleEdit:false, //编辑弹窗
      centerDialogVisibleAdd:false, //添加弹窗
      sexes:[{
        value: '男',
        label: 'Man'
      }, {
        value: '女',
        label: 'Woman'
      }],
      bloodTypes:[{
        value: 'A',
        label: 'A型'
      }, {
        value: 'B',
        label: 'B型'
      },{
        value: 'O',
        label: 'O型'
      }, {
        value: 'AB',
        label: 'AB型'
      }
      ]
    }
  },
  created() {
    this.getall()
  },
  methods:{
    getall(){
      request.get('donorinfo/page',{
        params: this.params
      }).then(res=>{
        if(res.code === '200'){
          this.tableData = res.data.list
          this.total=res.data.total
        }
      })
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 7,
        name:'',
        phone:''
      }
      this.getall()
    },
    handleCurrentChange(pageNum) {
      // 点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.getall()
    },
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.centerDialogVisibleEdit=true
    },
    addinfo(){
      this.centerDialogVisibleAdd=true
    },
    confirmadd(){
      request.post('donorinfo/addOne',this.addform).then(res=> {
        if (res.code === '200') {
          this.$message({
            type: "success",
            message: "添加信息成功！"
          })
          this.centerDialogVisibleAdd = false
          this.getall()
        } else {
          this.$message({
            type: "warning",
            message: res.msg
          })
          this.centerDialogVisibleAdd = false
          this.getall()
        }
      })
    },
    del(id){
      request.delete('donorinfo/deleteOne/'+id).then(res=>{
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.getall()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    async save(){
      if(this.form.id){
        this.$refs['ruleForm'].validate((valid) => {
          if(valid){
            request.put('donorinfo/updateOne',this.form).then(res=>{
                if(res.code === '200'){
                  this.$message({
                    type:"success",
                    message:"更新信息成功！"
                  })
                  this.centerDialogVisibleEdit=false
                  this.getall()
                }else{
                  this.$message({
                    type:"warning",
                    message: res.msg
                  })
                }
            })
          }
        })
      }
    },
    resetDateFilter() {
      this.$refs.filterTable.clearFilter('date');
    },
    clearFilter() {
      this.$refs.filterTable.clearFilter();
    },
    formatter(row, column) {
      return row.address;
    },
    filterTag(value, row) {
      return row.sex === value;
    },
    filterHandler(value, row, column) {
      const property = column['property'];
      return row[property] === value;
    }
  }
}

</script>
